package com.onlineMIS.action.headQ.inventory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.onlineMIS.ORM.DAO.Response;
import com.onlineMIS.ORM.entity.headQ.SQLServer.ClientsMS;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrder;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrderTagboxVO;
import com.onlineMIS.ORM.entity.headQ.user.UserInfor;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.loggerLocal;
import com.onlineMIS.converter.JSONSQLDateConverter;
import com.onlineMIS.converter.JSONUtilDateConverter;
import com.opensymphony.xwork2.ActionContext;

public class InventoryOrderJSONAction extends InventoryOrderAction {
	
	private JSONObject jsonObject;
	private JSONArray jsonArray;
	private Map<String,Object> jsonMap = new HashMap<String, Object>();
	public JSONArray getJsonArray() {
		return jsonArray;
	}
	public void setJsonArray(JSONArray jsonArray) {
		this.jsonArray = jsonArray;
	}
	public JSONObject getJsonObject() {
		return jsonObject;
	}
	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = -1720262593350813729L;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	
	
	/**
	 * copy the 
	 * @return
	 */
	public String copyOrder(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		String uuid = Common_util.getUUID();
		String log = logInventory("copyOrder", "-", formBean.getOrder().getOrder_ID(), uuid);
		loggerLocal.info(log);
		
		Response response = new Response();
		try {
		    response = inventoryService.copyOrder(loginUserInfor, formBean.getOrder());
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
			response.setMessage(e.getMessage());
		}
		jsonMap.put("response", response);
		
		loggerLocal.infoR(log +"," + response.getReturnCode());
		

		try{
		    jsonObject = JSONObject.fromObject(jsonMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "successful";
	}
	

	
	/**
	 * function to get the sqlserver client name when use the pinyin to do search
	 * @return
	 */
	public String getClientName(){
		loggerLocal.info("InventoryOrderAction-getClientName" );
		List<ClientsMS> clients = inventoryService.getClients(formBean.getPinyin());
		if (clients == null)
			clients = new ArrayList<ClientsMS>();
		
		jsonMap.put("clients", clients);
		
		try{
		    jsonObject = JSONObject.fromObject(jsonMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "successful";
	}
	
	/**
	 * 获取客户信息，主要是地区信息补充
	 * @return
	 */
	public String getClientRegion(){
		int clientId = formBean.getOrder().getClient_id();
		
		ClientsMS client = inventoryService.getClients(clientId);
		
		jsonMap.put("client", client);
		
		try{
		    jsonObject = JSONObject.fromObject(jsonMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "successful";		 
	}
	
	/**
	 * function to submitt the order from the PDA
	 * @return
	 */
	public String pdaSubmitOrder(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);

		String uuid = Common_util.getUUID();
		String log = logInventory("pdaSubmitOrder", formBean.getOrder().getClient_id(), formBean.getOrder().getOrder_ID(), uuid);
		loggerLocal.info(log);
		
		Response response = new Response();
		try{
			inventoryService.pdaSaveToInventory(formBean.getOrder(), loginUserInfor);
		    response.setReturnCode(Response.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			loggerLocal.error(formBean.getOrder().getClient_id() + "," + formBean.getOrder().getClient_name());
			loggerLocal.error(e);
			response.setQuickValue(Response.FAIL, e.getMessage());
		}
		
		loggerLocal.info(log + "," + response.getReturnCode());
		jsonMap.put("response", response);
		
		try{
		    jsonObject = JSONObject.fromObject(jsonMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "successful";
	}
	
	/**
	 * PDA submit the draft
	 * @return
	 */
	public String pdaSubmitDraft(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		String uuid = Common_util.getUUID();
		String log = logInventory("pdaSubmitDraft", formBean.getOrder().getClient_id(), formBean.getOrder().getOrder_ID(), uuid);
		loggerLocal.info(log);
		
		Response response = new Response();
		try{
		    response = inventoryService.pdaSaveToDraft(formBean.getOrder(), loginUserInfor);
		    if (formBean.getOrder().getOrder_ID() == 0)
		    	response.setQuickValue(Response.FAIL, "保存出现错误，请重新输入名字");
		} catch (Exception e) {
			loggerLocal.error(formBean.getOrder().getClient_id() + "," + formBean.getOrder().getClient_name());
			loggerLocal.error(e);
			response.setQuickValue(Response.FAIL, e.getMessage());
		}
		jsonMap.put("response", response);
		
		loggerLocal.info(log + "," + response.getReturnCode());
		
		try{
		    jsonObject = JSONObject.fromObject(jsonMap);
		} catch (Exception e) {
			e.printStackTrace();
			loggerLocal.error(e);
		}
		
		return "successful";
	}
	
	/**
	 * to delete the order by the confirmation
	 * @return
	 */
	public String deleteOrder(){
		int orderId = formBean.getOrder().getOrder_ID();
		String userName = formBean.getUser().getUser_name();
		String password = formBean.getUser().getPassword();
		
		String uuid = Common_util.getUUID();
		String log = logInventory("pdaSubmitDraft", "-", orderId, uuid);
		loggerLocal.info(log);
		
		Response  response = new Response();
		try {
			response = inventoryService.deleteByAuthorization(orderId, userName, password);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setQuickValue(Response.FAIL, e.getMessage());
		}
		
		loggerLocal.info(log + "," + response.getReturnCode());
		
		jsonMap.put("response", response);
		try{
		    jsonObject = JSONObject.fromObject(jsonMap);
		} catch (Exception e) {
			e.printStackTrace();
			loggerLocal.error(e);
		}
		
		return "successful";
	}
	
	/**
	 * To search the order based on the search criteria
	 * @return
	 */
	public String search(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		String uuid = Common_util.getUUID();
		String log = logInventory("search", "-", "-", uuid);
		loggerLocal.info(log);
		
		List<InventoryOrder> orderList = inventoryService.search(formBean,  this.getSort(), this.getOrder());
		jsonMap = inventoryService.constructInventoryOrderVOMap(orderList, loginUserInfor);


		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JSONUtilDateConverter());  

		try{
			   jsonObject = JSONObject.fromObject(jsonMap,jsonConfig);	
		   } catch (Exception e){
				loggerLocal.error(e);
			}
		
		loggerLocal.infoR(log);
		
		return "successful";
	}
	
	public String updateOrderComment(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		String uuid = Common_util.getUUID();
		String log = logInventory("search", "-", "-", uuid);
		loggerLocal.info(log);
		
		Response response = new Response();
		try{
			    response = inventoryService.updateOrderComment(formBean.getOrder());

		   } catch (Exception e){
			   response.setFail("更新单据备注错误 : " + e.getMessage());
				loggerLocal.error(e);
			} finally {
				jsonObject = JSONObject.fromObject(response);
			}
		
		loggerLocal.infoR(log);
		
		return "successful";
	}
	
	/**
	 * 因为单据具有权限控制,仓库分开操作以后必须有权限转移功能
	 * @return
	 */
	public String transferOrderToOther(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		String uuid = Common_util.getUUID();
		String log = logInventory("transferOrderToOther", "-", "-", uuid);
		loggerLocal.info(log);
		
		Response response = inventoryService.transferOrderToOther(formBean.getOrder().getOrder_ID(), loginUserInfor, formBean.getUser());
		try{
			   jsonObject = JSONObject.fromObject(response);
		   } catch (Exception e){
				loggerLocal.error(e);
			}
		
		
		return "successful";
	}
	private String logInventory(String action, Object clientId, Object orderId, String uuid){
		return super.logInventory(this.getClass().getSimpleName() , action, clientId, orderId, uuid);		
	}
	
	
	/**
	 * 打印单据
	 * @return
	 */
	public String printOrder(){
		Response response = inventoryService.getInventoryOrderForPrint(formBean.getOrder().getOrder_ID());
		try{
			   jsonObject = JSONObject.fromObject(response);
		   } catch (Exception e){
				loggerLocal.error(e);
			}

		return "successful";
	}
	
	/**
	 * 会计审核单据，如果是连锁店单据导入条码系统
	 * @return
	 * @throws Exception 
	 */
	public String acctAuditOrder() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		int orderId = formBean.getOrder().getOrder_ID();
		
		String uuid = Common_util.getUUID();
		String log = logInventory("acctAuditOrder", formBean.getOrder().getClient_id(), orderId, uuid);
		loggerLocal.info(log);
		
		Response response = new Response();
		try{
			   response = inventoryService.orderCompleteAudit(orderId, loginUserInfor);
			   jsonObject = JSONObject.fromObject(response);
		   } catch (Exception e){
			   response.setFail(e.getMessage());
			   e.printStackTrace();
			   loggerLocal.error(e);
			} finally {
				   jsonObject = JSONObject.fromObject(response);
			}

		return "successful";
	}
	
	/**
	 * inventory peopole save the order to draft
	 * 保存草稿
	 * @return
	 */
	public String saveToDraft() {

		String uuid = Common_util.getUUID();
		loggerLocal.info(logInventory("saveToDraft", formBean.getOrder().getClient_id(),
				formBean.getOrder().getOrder_ID(), uuid));
		UserInfor loginUserInfor = (UserInfor) ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		Response response = new Response();
		try {
			inventoryService.copyJSuserInfo(formBean.getOrder().getClient_id(), loginUserInfor);
		} catch (Exception e) {
			e.printStackTrace();
			loggerLocal.error("复制客户数据发生错误 : " + formBean.getOrder().getClient_id());
		}
		try {
			response = inventoryService.saveToDraft(formBean.getOrder(), formBean.getSorting(), loginUserInfor);
		} catch (Exception e) {
			response.setFail(e.getMessage());
			e.printStackTrace();
			loggerLocal.error(e);
		} finally {
			jsonObject = JSONObject.fromObject(response);
			loggerLocal.infoR(logInventory("saveToDraft", formBean.getOrder().getClient_id(),
					formBean.getOrder().getOrder_ID(), uuid));
		}

		return "successful";

	}
	
	/**
	 * 红冲单据
	 * @throws Exception 
	 */
	public String cancelOrder() throws Exception{
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		int orderId = formBean.getOrder().getOrder_ID();
		String uuid = Common_util.getUUID();
		String log = logInventory("cancelOrder", "-", orderId, uuid);
		loggerLocal.info(log);
		
		Response response = new Response();
		try{
			    response = inventoryService.cancel(loginUserInfor, orderId);

		   } catch (Exception e){
			   response.setFail(e.getMessage() + e.toString());
			   e.printStackTrace();
			   loggerLocal.error(e);
			} finally {
				   jsonObject = JSONObject.fromObject(response);
				   loggerLocal.infoR(log);
			}
		
		return "successful";

	}
	
	/*
	 * 导入到精算,还是需要审核
	 */
	public String exportToJinSuan() throws Exception{
		String uuid = Common_util.getUUID();
		String log = logInventory("exportToJinSuan", "-", formBean.getOrder().getOrder_ID(), uuid);
		loggerLocal.info(log);
		
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		Response response = new Response();
		try {
			response = inventoryService.saveToJinSuan(formBean.getOrder().getOrder_ID(),loginUserInfor);
			
		} catch(Exception e){
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		} finally {
			jsonObject = JSONObject.fromObject(response);
			loggerLocal.infoR(log);
		}

		return "successful";
	}
	
	
	/**
	 * the inventory submit the order to the accountants
	 * 提交单据到会计
	 * @return
	 */
	public String save(){
		String uuid = Common_util.getUUID();
		String log = logInventory("save()", "-",
				formBean.getOrder().getOrder_ID() + "," + formBean.getOrder().getClient_id(), uuid);
		loggerLocal.info(log);

		UserInfor loginUserInfor = (UserInfor) ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);

		Response response = new Response();
		try {
			inventoryService.copyJSuserInfo(formBean.getOrder().getClient_id(), loginUserInfor);
		} catch (Exception e) {
			e.printStackTrace();
			loggerLocal.error("复制客户数据发生错误 : " + formBean.getOrder().getClient_id());
		}
		try {
			response = inventoryService.inventoryComplsave(formBean, loginUserInfor);

		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		} finally {
			jsonObject = JSONObject.fromObject(response);
			loggerLocal.infoR(log);

		}
		
		return "successful";
	}
	
	/**
	 * 会计保存单据
	 * @return
	 */
	public String acctSave(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		String uuid = Common_util.getUUID();
		String log = logInventory("acctSave", formBean.getOrder().getClient_id(), formBean.getOrder().getOrder_ID(), uuid);
		loggerLocal.info(log);

		Response response = new Response();
		try {
			response =  inventoryService.acctEditAndSave(formBean,loginUserInfor);

		} catch(Exception e){
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		} finally {
			jsonObject = JSONObject.fromObject(response);
			loggerLocal.infoR(log);
		}
		
		return "successful";
	}	
	
	
	
	/**
	 * 打印pda单据
	 * @return
	 */
	public String printPDAorder(){
		String uuid = Common_util.getUUID();
		String log = logInventory("printPDAorder", formBean.getOrder().getClient_id(), formBean.getOrder().getOrder_ID(), uuid);
		loggerLocal.info(log);
		Response response = new Response();
		try{
			   response = inventoryService.printInventoryPDAorder(formBean.getOrder());
			   
		   } catch (Exception e){
				loggerLocal.error(e);
			} finally {
				jsonObject = JSONObject.fromObject(response);
			}

		return "successful";
	}
	
	/**
	 * 通过url获取绵阳服务器客户的库存
	 * @return
	 */
	public String checkClientInventory() {
		loggerLocal.info("InventoryOrderJSON.checkClientInventory");
		int clientId = formBean.getOrder().getCust().getId();
		String barcode = formBean.getBarcode();
		Response response = new Response();
		try{
			    response = inventoryService.checkClientInventory(clientId, barcode);

		   } catch (Exception e){
			   response.setFail(e.getMessage());
			   e.printStackTrace();
			   loggerLocal.error(e);
			} finally {
				   jsonObject = JSONObject.fromObject(response);
			}
		
		return "successful";
	}
	
	
	
	/**
	 * 查询客户标签分组json对象集合
	 * @return
	 */
	public String listOrderStatusTags() {
		loggerLocal.info("HeadQCustMgmtJSONAction.listCusTags");

		Response response = new Response();
		try {
			List<InventoryOrderTagboxVO> vos = inventoryService.initInventoryOrderStatus();

			JsonConfig jsonConfig = new JsonConfig();
    		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JSONUtilDateConverter());  
    		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JSONSQLDateConverter());  
			jsonArray = JSONArray.fromObject(vos,jsonConfig);
					
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}

		return "jsonArray";
	}
	
	
//	public String checkDummyInventory() {
//		loggerLocal.info("InventoryOrderJSONAction.checkDummyInventory");
//		Response response = new Response();
//		try{
//			   response = inventoryService.checkDummyOrders();
//			   
//		   } catch (Exception e){
//				loggerLocal.error(e);
//			} finally {
//				jsonObject = JSONObject.fromObject(response);
//			}
//
//		return "successful";
//	}
}



